﻿// Copyright (c) 2010 Dan Popick

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using Antlr.Runtime;
using Antlr.Runtime.Tree;

using Open.Web.Stijl.Parsing;
using Open.Web.Stijl.Css;

using System.Web;

using Map = System.Collections.Generic.Dictionary<string, byte>;

namespace Open.Web.Stijl
{
    public class CssParser
    {
        public List<object> Parse(string file) { return Parse(file, null); }

        public List<object> Parse(string file, List<string> dependentFiles)
        {
            List<object> components = ParseSingleFile(file);
            InjectInserts(file, components, dependentFiles);
            return components;
        }

        List<object> ParseSingleFile(string fileName)
        {
			using (Stream s = File.Open(fileName, FileMode.Open, FileAccess.Read))
			{
				ANTLRInputStream stream = new ANTLRInputStream(s);

				Css3Lexer l = new Css3Lexer(stream);
				CommonTokenStream tokenStream = new CommonTokenStream(l);
				Css3Parser p = new Css3Parser(tokenStream);

				p.stylesheet();
				return p.Components;
			}
        }

        void InjectInserts(string file, List<object> components, List<string> dependentFiles)
        {
            Map fileDict = new Map(StringComparer.OrdinalIgnoreCase);
            string baseDir = Path.GetDirectoryName(file);
            fileDict.Add(file, 0);

            int i = 0;
            while (i < components.Count)
            {
                Import imp = components[i] as Import;
                if (imp != null)
                {
                    string fileName = Path.Combine(baseDir, imp.Url.Unquote());
                    if (!fileDict.ContainsKey(fileName))
                    {
                        List<object> importComps = ParseSingleFile(fileName);
                        components.RemoveAt(i);
                        components.InsertRange(i, importComps);

                        fileDict.Add(fileName, 0);
                    }
                }
                else
                    i++;
            }

            if (dependentFiles != null)
                dependentFiles.AddRange(fileDict.Keys);
        }
    }
}
